package com.authine.cloudpivot.ext.applicationservice;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.ext.applicationservice.base.ApplicationBaseService;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.*;
import com.authine.cloudpivot.ext.dto.api.AgreeRefundVO;
import com.authine.cloudpivot.ext.dto.api.BulkRefundVO;
import com.authine.cloudpivot.ext.enums.order.*;
import com.authine.cloudpivot.ext.service.OrderService;
import com.authine.cloudpivot.ext.service.impl.OrderServiceImpl;
import com.authine.cloudpivot.ext.utils.BigDecimalUtils;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.cloudpivot.ext.utils.StringUtils;
import com.authine.cloudpivot.ext.utils.kingdee.DateUtils;
import com.authine.cloudpivot.ext.vo.TempOrderReceiptVo;
import com.authine.mvp.app.launcher.domain.utills.ApplicationContextUtils;
import com.authine.mvp.app.launcher.dto.bo.response.BO;
import com.authine.mvp.app.launcher.dto.bo.response.BOList;
import com.authine.mvp.app.org.dto.domainvo.DeptOrUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.util.CollectionUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

import static com.authine.hermes.app.launcher.service.ApplicationServiceFoundation.CUSTOM_SERVICE;

/**
 * @ClassName CustomerDetailService
 * @Description: 客户订单详情自定义服务
 * @Author cjj
 * @Date 2021/4/20
 * @Version V1.0
 **/
@Path("CustomerDetailService")
@Slf4j
public class CustomerDetailService extends ApplicationBaseService {
    private OrderService orderService = new OrderServiceImpl();
    private NamedParameterJdbcTemplate jdbcTemplate = ApplicationContextUtils.getBean(NamedParameterJdbcTemplate.class);

    /**
     * 客户订单详情页
     *
     * @param dto
     * @return
     */
    @Path("customerDetail")
    @POST
    @Operation(summary = "客户订单详情页", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<Map> customerDetail(@Parameter OrderCustomerDto dto) {
        log.info("------------------------客户订单详情页customerDetail Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单ID必填");
        }
        String orderVendorTableName = getTableName(ModelConst.T_ORDER_VENDOR);
        String orderCustomerGoodsTableName = getTableName(ModelConst.T_ORDER_CUSTOMER_GOODS);
        String orderGoodsReturnsTableName = getTableName(ModelConst.T_ORDER_GOODS_RETURNS);
        //客户订单信息
        Map<String, Object> orderCustomer = selectModelById(dto.getId(), ModelConst.T_ORDER_CUSTOMER);
        if (CollectionUtils.isEmpty(orderCustomer)) {
            return SingleResponse.buildFailure("-1", "客户订单不存在");
        }
        //设置客户订单状态
        String orderCustomerStatus = "";
        if (StringUtils.isNotBlank(orderCustomer.get("order_customer_status").toString())) {
            orderCustomerStatus = OrderCustomerStatusEnum.valueOf(orderCustomer.get("order_customer_status").toString()).des;
        }
        orderCustomer.put("order_customer_status", orderCustomerStatus);
        String orderVendorSql = "select * from " + orderVendorTableName + " where deleted ='0' and order_customer_id ='" + dto.getId() + "' order by createdTime desc";
        //供应商订单集合
        List<Map<String, Object>> orderVendorList = jdbcTemplate.queryForList(orderVendorSql, new HashMap<>());
        for (Map<String, Object> orderVendor : orderVendorList) {
            String orderCustomerGoodsSql = "select * from " + orderCustomerGoodsTableName + " where order_vendor_id ='" + orderVendor.get("id") + "' order by createdTime desc ";
            //客户订单商品集合
            List<Map<String, Object>> orderCustomerGoodsList = jdbcTemplate.queryForList(orderCustomerGoodsSql, new HashMap<>());
            for (Map<String, Object> orderCustomerGoods : orderCustomerGoodsList) {
                orderCustomerGoods.put("delivered_num", orderCustomerGoods.get("quantity_shipped"));
                orderCustomerGoods.put("received_num", orderCustomerGoods.get("quantity_received"));
                //退款商品数量
                orderCustomerGoods.put("refunded_num", this.getRefundedNum((String) orderCustomerGoods.get("id"), null));
                orderCustomerGoods.put("tax_rate",
                        ObjectUtil.defaultIfNull(BigDecimalUtils.round(BigDecimalUtils.mul((BigDecimal) orderCustomerGoods.get("tax_rate"), new BigDecimal("100")), 2).toPlainString() + "%", null));
                List<Map<String, Object>> orderGoodsReturnsList = JdbcTemplateUtils.builder(ModelConst.T_ORDER_GOODS_RETURNS).selectField("*")
                        .eq("order_customer_goods_id", orderCustomerGoods.get("id")).desc("createdTime").queryForList();
                //订单商品退货状态
                String orderGoodsReturnsStatus = "";
                if (!CollectionUtils.isEmpty(orderGoodsReturnsList)) {
                    orderGoodsReturnsStatus = (String) orderGoodsReturnsList.get(0).get("order_goods_returns_status");
                }
                orderCustomerGoods.put("order_goods_returns_status", orderGoodsReturnsStatus);
                String orderGoodsReturnsStatusName = "";
                if (StringUtils.isNotBlank(orderGoodsReturnsStatus)) {
                    orderGoodsReturnsStatusName = OrderGoodsReturnsStatusEnum.valueOf(orderGoodsReturnsStatus).des;
                }
                orderCustomerGoods.put("order_goods_returns_status_name", orderGoodsReturnsStatusName);
                List<Map<String, Object>> orderGoodsRelationList = JdbcTemplateUtils.builder(ModelConst.T_ORDER_GOODS_RELATION).selectField("*").eq("id", orderCustomerGoods.get("relation_id")).queryForList();
                if (!CollectionUtils.isEmpty(orderGoodsRelationList)) {
                    //送货要求
                    orderCustomerGoods.put("delivery_requirements", orderGoodsRelationList.get(0).get("delivery_requirements"));
                    //公司
                    orderCustomerGoods.put("company", orderGoodsRelationList.get(0).get("company"));
                    //仓库
                    orderCustomerGoods.put("warehouse", orderGoodsRelationList.get(0).get("warehouse"));
                }
//                Map<String, Object> orderVendorGoods = orderService.getOrderGoods((String) orderCustomerGoods.get("id"), null);
                //报价备注
//                orderCustomerGoods.put("description", CollectionUtils.isEmpty(orderVendorGoods) ? null : orderVendorGoods.get("remark_message"));

                //供应商订单状态
                orderCustomerGoods.put("order_vendor_status", orderVendor.get("order_vendor_status"));
                String orderVendorStatusName = "";
                if (StringUtils.isNotBlank((String) orderVendor.get("order_vendor_status"))) {
                    orderVendorStatusName = OrderVendorStatusEnum.valueOf((String) orderVendor.get("order_vendor_status")).des;
                }
                orderCustomerGoods.put("order_vendor_status_name", orderVendorStatusName);
                //供应商订单商品状态
                String orderCustomerGoodsStatusName = "";
                if (StringUtils.isNotBlank((String) orderCustomerGoods.get("order_customer_goods_status"))) {
                    orderCustomerGoodsStatusName = OrderGoodsStatusEnum.valueOf((String) orderCustomerGoods.get("order_customer_goods_status")).des;
                }
                orderCustomerGoods.put("order_customer_goods_status_name", orderCustomerGoodsStatusName);
                if (!Objects.isNull(orderCustomerGoods.get("package_quantity")) || !Objects.isNull(orderCustomerGoods.get("package_unit"))) {
                    Object package_quantity = BigDecimalUtils.round(BigDecimalUtils.isNull(orderCustomerGoods.get("package_quantity")), 2);
                    Object package_unit = ObjectUtil.defaultIfNull(orderCustomerGoods.get("package_unit"), "");
                    //包装数量/单位
                    orderCustomerGoods.put("package_quantity_unit", package_quantity + "/" + package_unit);
                }
                //补充澄清
//                Map<String, Object> extendsAttr = Objects.isNull(orderCustomerGoods.get("extends_attr")) ? null : JSON.parseObject((String) orderCustomerGoods.get("extends_attr"), HashMap.class);
                //实际收货金额（收货数量-退的收货部分的数量）*含税单价
                orderCustomerGoods.put("receive_amount", BigDecimalUtils.mul(BigDecimalUtils.sub(BigDecimalUtils.isNull(orderCustomerGoods.get("quantity_received")),
                        BigDecimalUtils.isNull(orderCustomerGoods.get("return_received_num"))), BigDecimalUtils.isNull(orderCustomerGoods.get("price"))));
            }
            orderVendor.put("grandson", orderCustomerGoodsList);
        }
        orderCustomer.put("children", orderVendorList);
        return SingleResponse.of(orderCustomer);
    }

    /**
     * 客户订单详情-修改留言
     *
     * @param dto
     * @return
     */
    @Path("customerUpdateMemo")
    @POST
    @Operation(summary = "客户订单详情-修改留言", tags = CUSTOM_SERVICE, description = "id必填、memo非必填")
    public SingleResponse<Boolean> customerUpdateMemo(@Parameter OrderVendorDto dto) {
        log.info("------------------------客户订单详情-修改留言 customerReceive Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "供应商订单ID必填");
        }
        Integer i = updateVendorMemo(dto.getId(), dto.getMemo());
        return SingleResponse.of(i >= 0 ? true : false);
    }

    /**
     * 客户订单详情-退款申请信息
     *
     * @param dto
     * @return
     */
    @Path("customerApplyRefundInfo")
    @POST
    @Operation(summary = "客户订单详情-退款申请信息", tags = CUSTOM_SERVICE, description = "ids必填")
    public SingleResponse<BOList> customerApplyRefundInfo(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-退款申请信息customerApplyRefundInfo Param：{}------------------------", JSONObject.toJSONString(dto));
        if (CollectionUtils.isEmpty(dto.getIds())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID集合必填");
        }
        Map<String, List<String>> param = new HashMap<>();
        param.put("order_customer_goods_id", dto.getIds());
        List<Map<String, Object>> goodsReturns = selectModelByIn(param, getTableName(ModelConst.T_ORDER_GOODS_RETURNS));
        if (CollectionUtils.isEmpty(goodsReturns)) {
            return SingleResponse.buildFailure("-1", "商品退货不存在");
        }
        log.info("------------------------客户订单详情-退款申请信息customerApplyRefundInfo Result：{}------------------------", JSONObject.toJSONString(goodsReturns));
        //构建结果集
        BOList bolist = new BOList();
        bolist.setData(goodsReturns);
        bolist.setTotal(goodsReturns.size());
        bolist.setSchemaCode(ModelConst.T_ORDER_GOODS_RETURNS);
        return SingleResponse.of(bolist);
    }

    /**
     * 客户订单详情-提交申请退款
     *
     * @param dto
     * @return
     */
    @Path("customerApplyRefund")
    @POST
    @Operation(summary = "客户订单详情-提交申请退款", tags = CUSTOM_SERVICE)
    public SingleResponse<BO> customerApplyRefund(@Parameter List<BulkRefundVO> dto) {
        log.info("------------------------客户订单详情-提交申请退款customerApplyRefund Param：{}------------------------", JSONObject.toJSONString(dto));
        if (CollectionUtils.isEmpty(dto)) {
            return SingleResponse.buildFailure("-1", "参数不合法");
        }
        List<Map<String, Object>> results = orderService.applyRefund(dto);
        if (CollectionUtils.isEmpty(results)) {
            return SingleResponse.buildFailure("-1", "请求失败");
        }else{
            return SingleResponse.buildSuccess();
        }
    }

    /**
     * 取消退款
     *
     * @param dto
     * @return
     */
    @Path("customerCancelRefund")
    @POST
    @Operation(summary = "客户订单详情-取消退款", tags = CUSTOM_SERVICE, description = "ids必填")
    public SingleResponse customerCancelRefund(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-取消退款customerCancelRefund Param：{}------------------------", JSONObject.toJSONString(dto));
        if (CollectionUtils.isEmpty(dto.getIds())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID集合必填");
        }
        List<String> orderVendorGoodIds = new ArrayList<>();
        for (String orderCustomerGoodsId : dto.getIds()) {
            Map<String, Object> orderVendor = orderService.getOrderGoods(orderCustomerGoodsId, null);
            orderVendorGoodIds.add((String) orderVendor.get("id"));
        }
        SingleResponse result = orderService.approvalRefund(new AgreeRefundVO(orderVendorGoodIds, false, null, null, null, null, null, "3"));
        log.info("------------------------客户订单详情-取消退款customerCancelRefund Result：{}------------------------", JSONObject.toJSONString(result));
        return result;
    }

    /**
     * 客户订单详情-查看物流
     *
     * @param dto
     * @return
     */
    @Path("customerLogistics")
    @POST
    @Operation(summary = "客户订单详情-查看物流", tags = CUSTOM_SERVICE, description = "id、pageIndex、pageSize必填")
    public SingleResponse<BOList> customerLogistics(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-查看物流customerLogistics Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填");
        }
        if (null == dto.getPageIndex() || null == dto.getPageSize()) {
            return SingleResponse.buildFailure("-1", "pageIndex、pageSize必填");
        }
        String tableName = getTableName(ModelConst.T_ORDER_TRACE);
        StringBuilder listSql = new StringBuilder();
        StringBuilder countSql = new StringBuilder();
        listSql.append("select * from " + tableName + " where deleted='0' and order_customer_goods_id ='" + dto.getId() + "' order by createdTime desc limit "
                + (dto.getPageIndex() - 1) * dto.getPageSize() + ", " + dto.getPageSize());
        countSql.append("select count(*) from " + tableName + " where deleted='0' and order_customer_goods_id ='" + dto.getId() + "'");
        List<Map<String, Object>> resultList = jdbcTemplate.queryForList(listSql.toString(), new HashMap<>());
        if (!CollectionUtils.isEmpty(resultList)) {
            for (Map<String, Object> objectMap : resultList) {
                String typeName = "";
                if (StringUtils.isNotBlank((String) objectMap.get("type"))) {
                    typeName = TraceTypeEnum.valueOf((String) objectMap.get("type")).des;
                }
                objectMap.put("type_name", typeName);
            }
        }
        int count = 0;
        try {
            count = jdbcTemplate.queryForObject(countSql.toString(), new HashMap<>(), Integer.class);
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        log.info("------------------------客户订单详情-查看物流customerLogistics Result：{}------------------------", JSONObject.toJSONString(resultList));
        //构建结果集
        BOList bOList = new BOList();
        bOList.setData(resultList);
        bOList.setTotal(count);
        bOList.setSchemaCode(ModelConst.T_ORDER_TRACE);
        return SingleResponse.of(bOList);
    }

    /**
     * 查看退款记录
     *
     * @param dto
     * @return
     */
    @Path("customerRefundRecord")
    @POST
    @Operation(summary = "客户订单详情-查看退款记录", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<Object> customerRefundRecord(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-查看退款记录customerRefundRecord Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填");
        }
        List<Map<String, Object>> resultList = JdbcTemplateUtils.builder(ModelConst.T_ORDER_GOODS_RETURNS).selectField("*")
                .eq("order_customer_goods_id", dto.getId()).sort("createdTime desc").queryForList();
        log.info("------------------------客户订单详情-查看退款记录customerRefundRecord Result：{}------------------------", JSONObject.toJSONString(resultList));
        return SingleResponse.of(!CollectionUtils.isEmpty(resultList) ? JSONObject.parseObject((String) resultList.get(0).get("extends_attr")) : null);
    }

    /**
     * 客户订单详情-上传收货单
     *
     * @param dto
     * @return
     */
    @Path("customerUploadReceive")
    @POST
    @Operation(summary = "客户订单详情-上传收货单", tags = CUSTOM_SERVICE, description = "ids、attachment必填")
    public SingleResponse<BO> customerUploadReceive(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-上传收货单 customerUploadReceive Param：{}------------------------", JSONObject.toJSONString(dto));
        if (CollectionUtils.isEmpty(dto.getIds())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID集合必填");
        }
        if (CollectionUtils.isEmpty(dto.getAttachment())) {
            return SingleResponse.buildFailure("-1", "收货单附件必填");
        }
        if (null == dto.getActualDeliverTime()){
            dto.setActualDeliverTime(DateUtils.dateToStr(new Date()));
        }
        uploadReceive(dto.getIds(), dto.getAttachment(), dto.getActualDeliverTime());
        return SingleResponse.buildSuccess();
    }

    /**
     * 客户订单详情-相关附件-收货单
     *
     * @param dto
     * @return
     */
    @Path("customerSaveReceive")
    @POST
    @Operation(summary = "客户订单详情-相关附件-收货单-保存", tags = CUSTOM_SERVICE, description = "id、attachment必填")
    public SingleResponse<Boolean> customerSaveReceive(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-相关附件-收货单-保存 customerUploadReceive Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填");
        }
        if (CollectionUtils.isEmpty(dto.getAttachment())) {
            return SingleResponse.buildFailure("-1", "收货单附件必填");
        }
        String attachment = JSONObject.toJSONString(dto.getAttachment());
        String updateSql = "update " + getTableName(ModelConst.T_ORDER_CUSTOMER_GOODS) + " set attachment = '" + attachment + "' where id = '" + dto.getId() + "'";
        Integer i = jdbcTemplate.update(updateSql, new HashMap<>());
        return SingleResponse.of(i >= 0 ? true : false);
    }

    /**
     * 客户订单详情-相关附件-收货单-修改实际交付日期
     *
     * @param dto
     * @return
     */
    @Path("customerUpdateActualDeliverTime")
    @POST
    @Operation(summary = "客户订单详情-相关附件-收货单-修改实际交付日期", tags = CUSTOM_SERVICE, description = "id、actualDeliverTime必填")
    public SingleResponse<Boolean> customerUpdateActualDeliverTime(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-相关附件-收货单-修改实际交付日期 customerUpdateActualDeliverTime Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填!");
        }
        if (StringUtils.isBlank(dto.getActualDeliverTime())) {
            return SingleResponse.buildFailure("-1", "实际交付日期必填!");
        }
        String actualDeliverTime = dto.getActualDeliverTime();
        String updateSql = "update " + getTableName(ModelConst.T_ORDER_CUSTOMER_GOODS) + " set actual_deliver_time = '" + actualDeliverTime + "' where id = '" + dto.getId() + "'";
        Integer i = jdbcTemplate.update(updateSql, new HashMap<>());
        return SingleResponse.of(i >= 0 ? true : false);
    }

    @Path("customerCheckImage")
    @POST
    @Operation(summary = "客户订单详情-维护图片-校验是否是采购商品",tags = CUSTOM_SERVICE,description = "goodsId必填")
    public SingleResponse<Boolean> customerCheckImage(@Parameter OrderVendorGoodsDto dto){
        log.info("------------------------客户订单详情-维护图片-校验是否是采购商品 customerCheckImage Param：{}------------------------", JSONObject.toJSONString(dto));
        if (null == dto || StringUtils.isBlank(dto.getGoodsId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填！");
        }
        //根据商品id获取采购目录商品信息
        Map<String, Object> goodsPurchase = JdbcTemplateUtils.builder(AppConst.COMMODITYMATERIALS,
                ModelConst.T_GOODS_PURCHASE).selectField("*").eq("id", dto.getGoodsId()).queryForMap();
        if (null ==goodsPurchase){
            return SingleResponse.buildFailure("-1", "该订单商品不是采购目录商品，无法维护图片！");
        }
        return SingleResponse.of(true);
    }

    /**
     * 客户订单详情-维护图片-更新图片
     *
     * @param dto
     * @return
     */
    @Path("customerDeleteImage")
    @POST
    @Operation(summary = "客户订单详情-维护图片-更新图片", tags = CUSTOM_SERVICE, description = "id、images必填")
    public SingleResponse<Boolean> customerDeleteImage(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-维护图片-更新图片 customerDeleteImage Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填");
        }
        if (CollectionUtils.isEmpty(dto.getImages())) {
            return SingleResponse.buildFailure("-1", "图片必填");
        }
        String images = JSONObject.toJSONString(dto.getImages());
        String updateSql = "update " + getTableName(ModelConst.T_ORDER_CUSTOMER_GOODS) + " set images = '" + images + "' where id = '" + dto.getId() + "'";
        Integer i = jdbcTemplate.update(updateSql, new HashMap<>());
        return SingleResponse.of(i >= 0 ? true : false);
    }

    /**
     * 客户订单详情-分批收货
     *
     * @param dto
     * @return
     */
    @Path("customerReceive")
    @POST
    @Operation(summary = "客户订单详情-分批收货", tags = CUSTOM_SERVICE, description = "id、quantity必填,单个商品收货")
    public SingleResponse<Boolean> customerReceive(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-收货 customerReceive Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填");
        }
        if (null == dto.getQuantity()) {
            return SingleResponse.buildFailure("-1", "数量必填");
        }
        Boolean result = orderService.goodsReceipt(dto.getId(), dto.getQuantity());
        return SingleResponse.of(result);
    }

    /**
     * 客户订单详情-整单收货
     *
     * @param dto
     * @return
     */
    @Path("customerBatchReceive")
    @POST
    @Operation(summary = "客户订单详情-整单收货", tags = CUSTOM_SERVICE, description = "ids必填，多个商品收货，代购合同-进行中-确认进度orderCustomerIds，必填")
    public SingleResponse<Boolean> customerBatchReceive(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单详情-收货 customerBatchReceive Param：{}------------------------", JSONObject.toJSONString(dto));
        if (!CollectionUtils.isEmpty(dto.getOrderCustomerIds())) {
            List<Map<String, Object>> orderCustomerGoods = JdbcTemplateUtils.builder(ModelConst.T_ORDER_CUSTOMER_GOODS).selectField("*").in("order_customer_id", dto.getOrderCustomerIds()).queryForList();
            List<String> orderCustomerGoodsIds = orderCustomerGoods.stream().map(t -> (String) t.get("id")).collect(Collectors.toList());
            dto.setIds(orderCustomerGoodsIds);
        }
        if (CollectionUtils.isEmpty(dto.getIds())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID集合必填");
        }
        //List<Map<String, Object>> result = orderService.goodsReceiptBatch(dto.getIds());
        TempOrderReceiptVo tempOrderReceiptVo = orderService.goodsReceiptBatch(dto.getIds());
        return SingleResponse.of(CollectionUtils.isEmpty(tempOrderReceiptVo.getOrderGoodsList()) ? false : true);
    }

    /**
     * 客户订单详情-供应商联系信息
     *
     * @param dto
     * @return
     */
    @Path("getVendorInfo")
    @POST
    @Operation(summary = "客户订单详情-供应商联系信息", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<BO> getVendorInfo(@Parameter DeptOrUserVO dto) {
        log.info("------------------------客户订单详情-供应商联系信息 getVendorInfo Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "ID必填");
        }
        String sql = "select * from " + AppConst.APP_COMMONSERVICE + "." + ModelConst.H_ORG_USER_BASE + " where deleted = '0' and id ='" + dto.getId() + "'";
        Map<String, Object> user = null;
        try {
            user = jdbcTemplate.queryForMap(sql, new HashMap<>());
        } catch (DataAccessException e) {
            e.printStackTrace();
        }
        if (null != user && !user.isEmpty()) {
            user.put("fullname", "管理员");
            user.put("cellphone", user.get("mobile"));
            user.put("email", user.get("email"));
        }
        BO bo = new BO();
        bo.setData(user);
        bo.setSchemaCode(ModelConst.H_ORG_USER_BASE);
        return SingleResponse.of(bo);
    }

    /**
     * 客户订单详情-填写退货物流
     *
     * @param dto
     * @return
     */
    @Path("customerTransportReturn")
    @POST
    @Operation(summary = "客户订单详情-填写退货物流", tags = CUSTOM_SERVICE, description = "orderCustomerGoodsIds、logistics、waybill必填")
    public SingleResponse<BO> customerTransportReturn(@Parameter OrderTraceDto dto) {
        log.info("------------------------客户订单详情-填写退货物流 customerTransportReturn Param：{}------------------------", JSONObject.toJSONString(dto));
        if (CollectionUtils.isEmpty(dto.getOrderCustomerGoodsIds())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID集合必填");
        }
        if (StringUtils.isBlank(dto.getLogistics())) {
            return SingleResponse.buildFailure("-1", "物流必填");
        }
        if (StringUtils.isBlank(dto.getWaybill())) {
            return SingleResponse.buildFailure("-1", "运单号必填");
        }
        orderService.goodsReturn(dto.getOrderCustomerGoodsIds(), dto.getLogistics(), dto.getWaybill());
        return SingleResponse.buildSuccess();
    }

    /**
     * 订单商品详情-移动端
     *
     * @param dto
     * @return
     */
    @Path("customerGoodsDetail")
    @POST
    @Operation(summary = "商品订单详情-移动端", tags = CUSTOM_SERVICE, description = "id必填")
    public SingleResponse<Map> customerGoodsDetail(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------客户订单商品详情页customerGoodsDetail Param：{}------------------------", JSONObject.toJSONString(dto));
        if (StringUtils.isBlank(dto.getId())) {
            return SingleResponse.buildFailure("-1", "客户订单商品ID必填");
        }
        //客户订单商品信息
        Map<String, Object> orderCustomerGoods = selectModelById(dto.getId(), ModelConst.T_ORDER_CUSTOMER_GOODS);
        if (CollectionUtils.isEmpty(orderCustomerGoods)) {
            return SingleResponse.buildFailure("-1", "客户订单商品不存在");
        }
        //供应商订单商品状态
        String orderCustomerGoodsStatusName = "";
        if (StringUtils.isNotBlank((String) orderCustomerGoods.get("order_customer_goods_status"))) {
            orderCustomerGoodsStatusName = OrderGoodsStatusEnum.valueOf((String) orderCustomerGoods.get("order_customer_goods_status")).des;
        }
        orderCustomerGoods.put("order_customer_goods_status_name", orderCustomerGoodsStatusName);
        Map<String, Object> orderCustomer = selectModelById((String) orderCustomerGoods.get("order_customer_id"), ModelConst.T_ORDER_CUSTOMER);
        orderCustomerGoods.put("orderCustomer", orderCustomer);
        return SingleResponse.of(orderCustomerGoods);
    }

    /**
     * 收货并上传收货单-移动端
     *
     * @param dto
     * @return
     */
    @Path("customerReceiveAndUpload")
    @POST
    @Operation(summary = "收货并上传收货单-移动端", tags = CUSTOM_SERVICE, description = "id, quantity必填, attachment上传单非必填")
    public SingleResponse<Boolean> customerReceiveAndUpload(@Parameter OrderCustomerGoodsDto dto) {
        log.info("------------------------收货并上传收货单customerReceiveAndUpload Param：{}------------------------", JSONObject.toJSONString(dto));
        if (!CollectionUtils.isEmpty(dto.getAttachment())) {
            uploadReceive(Collections.singletonList(dto.getId()), dto.getAttachment(), dto.getActualDeliverTime());
        }
        Boolean result = orderService.goodsReceipt(dto.getId(), dto.getQuantity());
        return SingleResponse.of(result);
    }

    /**
     * updateOrderStatus
     *
     * @param
     * @return
     */
    @Path("getDefaultBankAccount")
    @POST
    @Operation(summary = "获取默认银行账号", tags = CUSTOM_SERVICE, description = "无")
    public SingleResponse<BO> getDefaultBankAccount() {
        List<Map<String, Object>> result = getBankAccount(null, true, null);
        BO bo = new BO();
        bo.setData(CollectionUtils.isEmpty(result) ? null : result.get(0));
        bo.setSchemaCode(ModelConst.T_MEMBER_DELIVER_ADDRESS);
        return SingleResponse.of(bo);
    }
}
